from fastapi import APIRouter, Request, HTTPException, Header, Response
from fastapi.responses import FileResponse

from backend.const import *
from backend.error import error_pydantic_msg
from backend.exception import DuplicateError
# TODO code review: import引入规范，如果超过一行表示，请使用括号进行分组，不要用反斜杠"\"
#from backend.handler import HandleDataInfo, HandleDataSelectInfo, HandleDataAdd, HandleDataCard, HandleDataViewer, \
#    HandleDataVisual, HandleDataSearch
from backend.handler import (
    HandleDataInfo, 
    HandleDataSelectInfo, 
    HandleDataSearch,
    HandleDataAdd, 
    HandleDataCard, 
    HandleDataViewer, 
    HandleDataVisual, 
)
from backend.model import *
from common.response import OkResponse, OkDuplicateAddResponse, BadRequestResponse, ServerErrorResponse


router = APIRouter()


@router.get("/info")
def data_info(page_id: int = 1, page_size: int = 5, page_total: int = 0):
    # 校验参数是否正确 page_size 不能为0，否则查询无意义
    if page_size <= 0:
        page_size = DEFAULT_PAGE_SIZE
    if page_id < 1:
        page_id = DEFAULT_PAGE_ID  # page_id默认页码号从1开始
    try:
        return OkResponse(data=HandleDataInfo(page_id=page_id, page_size=page_size, page_total=page_total))
    except ValidationError as e:
        import traceback
        traceback.print_exc()  # 联调用
        return ServerErrorResponse(data=error_pydantic_msg(e))
    except Exception as e:
        print(repr(e))
        return ServerErrorResponse(data=repr(e))


@router.get("/selectinfo")
def data_selectinfo():
    try:
        return OkResponse(data=HandleDataSelectInfo())
    except ValidationError as e:
        print(e.errors())
        return ServerErrorResponse(data=error_pydantic_msg(e))
    except Exception as e:
        return ServerErrorResponse(data=str(e))


@router.get("/search")
def data_search(name: str):
    print("search_name:", name)
    if not name:
        return BadRequestResponse(data="name empty error") 
    try:
        return OkResponse(data=HandleDataSearch(name))
    except ValidationError as e:
        return ServerErrorResponse(data=error_pydantic_msg(e))
    except Exception as e:
        return ServerErrorResponse(data=str(e))
        

@router.post("/add")
def data_add(post_body: DataSource):
    print(post_body)
   
    try:
        return OkResponse(data=HandleDataAdd(post_body))
    except DuplicateError as e:
        return OkDuplicateAddResponse()
    except ValidationError as e:
        return ServerErrorResponse(data=error_pydantic_msg(e))
    except Exception as e:
        return ServerErrorResponse(data=repr(e))


@router.get("/card")
def data_card(id: int):
    print(id)
    try:
        return FileResponse(path=HandleDataCard(id), media_type='text/plain,text/html,text/xml')
    except DuplicateError as e:
        return OkDuplicateAddResponse()
    except ValidationError as e:
        return ServerErrorResponse(data=error_pydantic_msg(e))
    except Exception as e:
        return ServerErrorResponse(data=repr(e))


@router.get("/viewer")
def data_viewer(id: int):
    try:
        return OkResponse(data=HandleDataViewer(id=id))
    except DuplicateError as e:
        return OkDuplicateAddResponse()
    except ValidationError as e:
        print(repr(e))
        return ServerErrorResponse(data=error_pydantic_msg(e))
    except Exception as e:
        print(repr(e))
        return ServerErrorResponse(data=repr(e))


@router.get("/visual")
def data_visual(id: int):
    print(id)
    try:
        return OkResponse(data=HandleDataVisual(id=id))
    except ValidationError as e:
        return ServerErrorResponse(data=error_pydantic_msg(e))
    except Exception as e:
        return ServerErrorResponse(repr(e))
